# -*- shell-script -*-

# 00minimal - lsvio routines with /proc/device-tree/vdevice.

# This file is part of the Linux lsvpd package.

# (C) Copyright IBM Corp. 2005

# Maintained by Martin Schwenke <martins@au.ibm.com>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    
# $Id: 30device-tree,v 1.1 2006/04/11 18:38:28 emunson Exp $

[ -d "${db_bus_dt_dir}/vdevice" ] || return 0

######################################################################

usage ()
{
    cat <<EOF 1>&2
usage: $0 [options]
options: -h      print this usage message
         -s      List virtual SCSI adapters
         -e      List virtual Ethernet adapters
         -d      List virtual SCSI devices and associated adapters
	 -D db   Directory db contains database, instead of default.
	 -z tgz  File tgz contains database archive (overrides -d).
At least one of -s, -e, -d must be given.
EOF
    exit 1
}

process_options ()
{
    # Defaults.
    list_scsi_adapters=false
    list_ethernet_adapters=false
    list_scsi_devices=false

    options=":hsed${db_path_option}:z:"
    while getopts ${options} opt "$@" ; do
	case ${opt} in
	    (h) usage                       ;;
	    (s) list_scsi_adapters=true     ;;
	    (e) list_ethernet_adapters=true ;;
	    (d) list_scsi_devices=true      ;;
	    (${db_path_option}|z) :  ;; # Done by process_db_path_options().
	    \?)
                echo "unknown option \`${OPTARG}'" 1>&2
		echo 1>&2
		usage
		;;
	esac
    done

    shift $((${OPTIND} - 1))
    # No extra arguments allowed.
    [ -n "$1" ] && usage
    # At least one flag set.
    $list_scsi_adapters || \
	$list_ethernet_adapters || \
	$list_scsi_devices || \
	usage

    return 0
}

######################################################################

list_linux_vpd ()
{
    list_linux_vpd_basic "${db_bus_dt_dir}/vdevice"
}

render_all ()
{
    local adapter_output=""
    local device_output=""

    render_all_basic

    echo -n "$adapter_output"
    echo -n "$device_output"
}

render_linux_vpd ()
{
    local x="$1"

    local d="${x%/linux,vpd}"
    case "${d##*/}" in
	(v-scsi@*) $list_scsi_adapters     && lsvio_render_adapter      "$x" ;;
	(l-lan@*)  $list_ethernet_adapters && lsvio_render_adapter      "$x" ;;
	(*)        $list_scsi_devices      && lsvio_render_vscsi_device "$x" ;;
    esac
}

lsvio_render_adapter ()
{
    local d="$1"

    local node="${d%/linux,vpd}"

    local nl="
"

    local vpd_subdirs
    vpd_subdirs_list_hook "$node"
    local vpd_dir
    for vpd_dir in $vpd_subdirs ; do
	local vpd_value
	vpd_field_get "$vpd_dir" "AX"
	local ax="$vpd_value"
	vpd_field_get "$vpd_dir" "YL"
	local yl="$vpd_value"
	[ -n "$ax" -a -n "$yl" ] && \
	    adapter_output="${adapter_output}${ax} ${yl}${nl}"
    done
}

lsvio_render_vscsi_device ()
{
    local d="$1"

    local node="${d%/linux,vpd}"

    local nl="
"

    l="${node}/lsvpd,parent-node"
    if [ -L "$l" ] ; then
	local vpd_dir vpd_value
	vpd_dir_set_hook "$l"
	vpd_field_get "$vpd_dir" "AX"
	local adapter_ax="$vpd_value"
	if [ -n "$adapter_ax" ] ; then
	    local vpd_subdirs
	    vpd_subdirs_list_hook "$node"
	    for vpd_dir in $vpd_subdirs ; do
		vpd_field_get "$vpd_dir" "AX"
		local ax="$vpd_value"
		[ -n "$ax" ] && \
		    device_output="${device_output}${adapter_ax} ${ax}${nl}"
	    done
	fi
    fi
}
